<template>
  <div class="app-container">
    <el-row :gutter="10">
      <el-col :span="24" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="商品编号" prop="itemNo">
            <el-input
              v-model="queryParams.itemNo"
              placeholder="请输入商品编号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="商品名称" prop="itemName">
            <el-input
              v-model="queryParams.itemName"
              placeholder="请输入商品名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <!--<el-form-item label="商品类别" prop="categoryId">
            <el-input
              v-model="queryParams.categoryId"
              placeholder="请选择商品类别"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>-->

          <el-form-item label="商品类别" prop="categoryId">
            <el-select
              v-model="queryParams.categoryId"
              placeholder="请选择商品类别"
            >
              <el-option
                v-for="item in categoryOptions"
                :key="item.categoryId"
                :label="item.categoryName"
                :value="item.categoryId"
                @keyup.enter.native="handleQuery"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
              重置
            </el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="3">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="small"
              @click="handleAdd"
              >新增商品</el-button
            >
          </el-col>
        </el-row>

        <el-table v-loading="loading" :data="itemList">
          <el-table-column label="ID" align="center" prop="itemId" width="55" />
          <el-table-column label="商品编号" align="center" prop="itemNo" />
          <el-table-column label="商品名称" align="center" prop="itemName" />
          <el-table-column label="商品库存" align="center" prop="inventory" />
          <el-table-column label="商品类型" align="center" prop="categoryId" />
          <el-table-column label="商品价格" align="center" prop="price" />
          <el-table-column
            label="类别编号"
            align="center"
            prop="category.categoryNo"
          />
          <el-table-column
            label="类别名称"
            align="center"
            prop="category.categoryName"
          />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="small"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                >修改</el-button
              >
              <el-button
                size="small"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!--设置分页开始-->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          @pagination="getList"
          :current-page="pageNum"
          :page-sizes="[5, 10, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
        <!--设置分页结束-->
      </el-col>
    </el-row>
    <!-- 添加修改商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品编号" prop="itemNo">
          <!--<el-input v-model="form.itemNo" placeholder="请输入商品编号" />-->
          <el-select
            v-model="queryParams.categoryId"
            placeholder="请选择商品类别"
          >
            <el-option
              v-for="item in categoryOptions"
              :key="item.categoryId"
              :label="item.categoryName"
              :value="item.categoryId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品名称" prop="itemName">
          <el-input v-model="form.itemName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品库存" prop="inventory">
          <el-input v-model="form.inventory" placeholder="请输入商品库存" />
        </el-form-item>
        <el-form-item label="商品类别" prop="categoryId">
          <el-input v-model="form.categoryId" placeholder="请输入商品类别" />
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入商品价格" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//import { listItem, getItem, addItem, updateItem, deleteItem } from "@/api/posItem";
import {
  listItemByPage,
  getItem,
  addItem,
  updateItem,
  deleteItem,
} from "@/api/posItem";
import { listCategory } from "@/api/category";
export default {
  name: "Item",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 商品表格数据
      itemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 总条数
      total: 0,
      // 当前页
      pageNum: 0,
      // 每页数据量
      pageSize: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        itemNo: undefined,
        itemName: undefined,
        categoryId: undefined,
      },
      categoryOptions: undefined,
      // 表单参数
      form: {},
      rules: {
        itemNo: [
          { required: true, message: "商品编号不能为空", trigger: "blur" },
        ],
        itemName: [
          { required: true, message: "商品名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    console.log(13);
    this.getList();
    listCategory().then((response) => {
      this.categoryOptions = response.data;
    });
  },
  methods: {
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listItemByPage(this.queryParams).then((response) => {
        this.itemList = response.data.list;
        this.total = response.data.total;
        this.pageNum = response.data.pageNum;
        this.pageSize = response.data.pageSize;
        this.loading = false;
      });
    },
    //分页相关脚本
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      console.log(val);
      this.getList();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      console.log(val);
      this.getList();
    },

    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.resetForm();
    },

    /** 表单重置 */
    resetForm() {
      this.form = {
        itemId: undefined,
        itemNo: undefined,
        itemName: undefined,
        inventory: undefined,
        categoryId: undefined,
        price: undefined
      };
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
      this.$message.success("查询成功");
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize:5,
        itemNo: undefined,
        itemName: undefined,
        categoryId: undefined,
      };
      this.getList();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.resetForm();
      this.open = true;
      this.title = "添加商品";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.resetForm();
      getItem(row.itemId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品";
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.title == "修改商品") {
            updateItem(this.form).then((response) => {
              this.open = false;
              this.title = "";
              this.getList();
              this.$message.success(response.data); //后端返回消息字符串
            });
          } else {
            addItem(this.form).then((response) => {
              this.open = false;
              this.title = "";
              this.getList();
              this.$message.success(response.data);
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm(
        '是否确认删除名称为"' + row.itemName + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return deleteItem(row.itemId);
        })
        .then(() => {
          this.getList();
          this.$message.success("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
